package com.hl.qzzpbackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hl.qzzpbackend.common.DeleteRequest;
import com.hl.qzzpbackend.exception.BusinessException;
import com.hl.qzzpbackend.exception.ErrorCode;
import com.hl.qzzpbackend.exception.ThrowUtils;
import com.hl.qzzpbackend.model.dto.jobExpectation.JobExpectationAddRequest;
import com.hl.qzzpbackend.model.dto.jobExpectation.JobExpectationUpdateRequest;
import com.hl.qzzpbackend.model.entity.JobExpectation;
import com.hl.qzzpbackend.model.entity.PositionType;
import com.hl.qzzpbackend.model.entity.User;
import com.hl.qzzpbackend.model.vo.JobExpectationVO;
import com.hl.qzzpbackend.service.JobExpectationService;
import com.hl.qzzpbackend.mapper.JobExpectationMapper;
import com.hl.qzzpbackend.service.PositionTypeService;
import com.hl.qzzpbackend.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 求职期望业务层
 * @author helong
 */
@Service
public class JobExpectationServiceImpl extends ServiceImpl<JobExpectationMapper, JobExpectation>
    implements JobExpectationService{

    @Resource
    private UserService userService;

    @Resource
    private PositionTypeService positionTypeService;

    /**
     * 新增求职期望
     * @param jobExpectationAddRequest 求职期望新增请求
     * @return 是否成功
     */
    @Override
    public boolean jobExpectationAdd(JobExpectationAddRequest jobExpectationAddRequest) {
        Long userId = jobExpectationAddRequest.getUserId();
        Long positionTypeId = jobExpectationAddRequest.getPositionTypeId();

        // 参数校验
        long count1 = userService.lambdaQuery().eq(User::getId, userId).count();
        ThrowUtils.throwIf(count1 == 0, ErrorCode.PARAMS_ERROR,"用户不存在");
        long count2 = positionTypeService.lambdaQuery().eq(PositionType::getId, positionTypeId).count();
        ThrowUtils.throwIf(count2 == 0, ErrorCode.PARAMS_ERROR,"岗位类型不存在");

        JobExpectation jobExpectation = new JobExpectation();
        BeanUtil.copyProperties(jobExpectationAddRequest, jobExpectation);

        boolean result = this.save(jobExpectation);
        if(!result){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"新增求职期望失败");
        }
        return true;
    }

    /**
     * 更新求职期望
     * @param jobExpectationUpdateRequest 求职期望更新请求
     * @return 是否成功
     */
    @Override
    public boolean jobExpectationUpdate(JobExpectationUpdateRequest jobExpectationUpdateRequest) {
        Long id = jobExpectationUpdateRequest.getId();
        Long userId = jobExpectationUpdateRequest.getUserId();
        Long positionTypeId = jobExpectationUpdateRequest.getPositionTypeId();

        // 参数校验
        long count1 = userService.lambdaQuery().eq(User::getId, userId).count();
        ThrowUtils.throwIf(count1 == 0, ErrorCode.PARAMS_ERROR,"用户不存在");
        long count2 = positionTypeService.lambdaQuery().eq(PositionType::getId, positionTypeId).count();
        ThrowUtils.throwIf(count2 == 0, ErrorCode.PARAMS_ERROR,"岗位类型不存在");
        long count3 = this.lambdaQuery().eq(JobExpectation::getId, id).count();
        ThrowUtils.throwIf(count3 == 0, ErrorCode.PARAMS_ERROR,"求职期望不存在");

        JobExpectation jobExpectation = new JobExpectation();
        BeanUtil.copyProperties(jobExpectationUpdateRequest, jobExpectation);
        boolean result = this.updateById(jobExpectation);
        if(!result){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"更新求职期望失败");
        }
        return true;
    }

    /**
     * 删除求职期望
     * @param deleteRequest 删除请求
     * @return 是否成功
     */
    @Override
    public boolean jobExpectationDelete(DeleteRequest deleteRequest) {
        Long id = deleteRequest.getId();

        // 参数校验
        long count = this.lambdaQuery().eq(JobExpectation::getId, id).count();
        ThrowUtils.throwIf(count == 0, ErrorCode.PARAMS_ERROR,"求职期望不存在");

        boolean result = this.removeById(id);
        if(!result){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"删除求职期望失败");
        }
        return true;
    }


    /**
     * 获取求职期望列表
     * @param userId 用户id
     * @return 求职期望列表
     */
    @Override
    public List<JobExpectationVO> getJobExpectationList(Long userId) {
        long count = userService.lambdaQuery().eq(User::getId, userId).count();
        ThrowUtils.throwIf(count == 0, ErrorCode.PARAMS_ERROR,"用户不存在");

        List<JobExpectation> jobExpectationList = this.lambdaQuery()
                .eq(JobExpectation::getUserId, userId)
                .orderByAsc(JobExpectation::getCreateTime)
                .list();
        if(CollUtil.isEmpty(jobExpectationList)){
            return Collections.emptyList();
        }
        return jobExpectationList.stream().map(jobExpectation -> {
            JobExpectationVO jobExpectationVO = new JobExpectationVO();
            BeanUtil.copyProperties(jobExpectation, jobExpectationVO);
            return jobExpectationVO;
        }).collect(Collectors.toList());
    }
}




